#ifndef NODES_H
#define NODES_H

#include <string.h>
#include "Def.h"

class Nodes{
		
	public:
		// nodes and terminals		
		NODE_SET nodes; 
		NODE_SET terminals; 
		
		//Destructor
		~Nodes(){
			vector<Node *> gnodes(nodes.size()+terminals.size());
			for(NODE_ITERATOR it=nodes.begin(); it!=nodes.end(); ++it){
				gnodes.push_back(*it);
			}
			nodes.clear();
			for(NODE_ITERATOR it=terminals.begin(); it!=terminals.end(); ++it){
				gnodes.push_back(*it);
			}
			terminals.clear();
			for(uint32_t i=0; i<gnodes.size(); ++i)
				delete gnodes[i];
			gnodes.clear();
		}
		
		bool parse(string fileName){
		
			ifstream inp(fileName.c_str());
			if(!inp.is_open()){
				cerr<<"Can not open "<<fileName<<endl;
				exit(1);
			}
			int numArg;
			int numNodes, numTerminals;
			char line[1024];
			char name[256], type[256];
			Node *node;
			
			for(int i=0; i<4; ++i)
				inp.getline(line, 1024);
				
			inp.getline(line, 1024);
			sscanf(line,"NumNodes      :  %d", &numNodes);
			printf("Number of Nodes      :  %d\n", numNodes);
			inp.getline(line, 1024);
			sscanf(line,"NumTerminals      :  %d", &numTerminals);
			printf("Number of Terminals      :  %d", numTerminals);
			inp.getline(line, 1024);
			
			for(int i=0; i<numNodes; ++i){
				inp.getline(line, 1024);
				node = new Node();
				numArg =sscanf(line,"             %s             %d             %d   %s", name, &node->width, &node->height, type);
				node->name = name;
				nodes.insert(node);
				if(numArg==4){
					if(!strcmp(type,"terminal"))
						node->nodes_movetype = terminal;
					else
						node->nodes_movetype = terminal_NI;
				}
			}
			
			
			inp.close();
			return true;
		}
	};	
	
	extern Nodes nodes;			
				
#endif

